package com.aso114.project.mvp.activity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aso114.project.R;
import com.aso114.project.base.BaseMvpActivity;
import com.aso114.project.base.BaseSimpleActivity;
import com.aso114.project.mvp.activity.fragment.MainIndexFragment;
import com.aso114.project.mvp.activity.fragment.MainThreeFragment;
import com.aso114.project.mvp.activity.fragment.MainTwoFragment;
import com.aso114.project.mvp.presenter.MainPresenter;
import com.aso114.project.mvp.view.MainView;
import com.aso114.project.util.Helper;
import com.aso114.project.util.PresenterFactory;
import com.aso114.project.util.PresenterLoder;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * username:Created by hsj on 2017/2/12 0012.
 * class：
 * 类描述：启动页
 */

public class MainActivity extends BaseSimpleActivity {
    public static final String REFRESH = "MyFragment" + "refresh";

    @BindView(R.id.fragment_home)
    RelativeLayout fragmentHome;
    @BindView(R.id.home_btn_bar_iv)
    ImageView homeBtnBarIv;
    @BindView(R.id.home_btn_bar_tv)
    TextView homeBtnBarTv;
    @BindView(R.id.home_btn_bar_ll)
    LinearLayout homeBtnBarLl;
    @BindView(R.id.need_btn_bar_iv)
    ImageView needBtnBarIv;
    @BindView(R.id.need_btn_bar_tv)
    TextView needBtnBarTv;
    @BindView(R.id.need_btn_bar_ll)
    LinearLayout needBtnBarLl;
    @BindView(R.id.my_btn_bar_iv)
    ImageView myBtnBarIv;
    @BindView(R.id.my_btn_bar_tv)
    TextView myBtnBarTv;
    @BindView(R.id.my_btn_bar_ll)
    LinearLayout myBtnBarLl;

    private MainIndexFragment homeFragment;
    private MainTwoFragment needFragment;
    private MainThreeFragment myFragment;

    @Override
    protected int getResouseId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        showMainIndexFragment();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void getBroadcast(Context context, Intent intent) {
    }

    public  void showMainIndexFragment() {
        initBottom();
        homeBtnBarIv.setImageResource(R.mipmap.home_lianxi_ic);
        homeBtnBarTv.setTextColor(getResources().getColor(R.color.aff6559));
        if (null == homeFragment) {
            homeFragment = new MainIndexFragment();
        }
        Helper.changeFragment(this, R.id.fragment_home, homeFragment);
    }

    public void showMainTwoFragment() {
        initBottom();
        needBtnBarIv.setImageResource(R.mipmap.home_lxtm_ic);
        needBtnBarTv.setTextColor(getResources().getColor(R.color.aff6559));
        if (null == needFragment) {
            needFragment = new MainTwoFragment();
        }
        Helper.changeFragment(this, R.id.fragment_home, needFragment);
    }

    public void showMainThreeFragment() {
        initBottom();
        myBtnBarIv.setImageResource(R.mipmap.home_myh_ic);
        myBtnBarTv.setTextColor(getResources().getColor(R.color.aff6559));
        if (null == myFragment) {
            myFragment = new MainThreeFragment();
        }
        Helper.changeFragment(this, R.id.fragment_home, myFragment);
    }

    private void initBottom() {
        //图标
        homeBtnBarIv.setImageResource(R.mipmap.home_tik_ic);
        needBtnBarIv.setImageResource(R.mipmap.home_xizhi_ic);
        myBtnBarIv.setImageResource(R.mipmap.home_wode_ic);
        //文字
        homeBtnBarTv.setTextColor(getResources().getColor(R.color.a999999));
        needBtnBarTv.setTextColor(getResources().getColor(R.color.a999999));
        myBtnBarTv.setTextColor(getResources().getColor(R.color.a999999));
    }

    /**
     * 双击退出当前界面
     */
    private long temp;

    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - temp < 2000) {
            super.onBackPressed();

            if(LoginActivity.instance!=null){
                LoginActivity.instance.finish();
            }
            finish();
        } else {
            temp = System.currentTimeMillis();
            Helper.showToast("再按一次退出");
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.home_btn_bar_ll, R.id.need_btn_bar_ll, R.id.my_btn_bar_ll})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.home_btn_bar_ll:
                showMainIndexFragment();
                break;
            case R.id.need_btn_bar_ll:
                showMainTwoFragment();
                break;
            case R.id.my_btn_bar_ll:
                showMainThreeFragment();
                break;
        }
    }
}
